package cn.cvs.controller;

import cn.cvs.entity.SysRole;
import cn.cvs.entity.SysUser;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.utils.Constants;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 角色模块控制器
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    Logger logger = Logger.getLogger(SysRoleController.class);
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询角色列表
     * @param model
     * @return
     */
    @GetMapping(value="/list")
    public String getRoleList(Model model)throws Exception{
        List<SysRole> roleList = sysRoleService.getRoleList();
        model.addAttribute("roleList", roleList);
        return "sysRole/list";
    }

    /**
     * 查寻简单角色列表
     * @return
     */
    @GetMapping(value = "/simpleList",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object getRoleList()throws Exception{
        List<SysRole> roleList = sysRoleService.getRoleList();
        return JSONArray.toJSONString(roleList);
    }

    /**
     * 跳转道添加角色页面
     * @param sysRole
     * @return
     */
    @GetMapping(value="/toAdd")
    public String addRole(@ModelAttribute("role") SysRole sysRole){
        return "sysRole/add";
    }

    /**
     * 添加角色
     * @param sysRole
     * @param session
     * @return
     */
    @PostMapping(value="/add")
    public String addRoleSave(SysRole sysRole,HttpSession session)throws Exception{
        sysRole.setCreatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        if(sysRoleService.add(sysRole)){
            return "redirect:/sys/role/list";
        }
        return "sysRole/add";
    }

    /**
     * 跳转到修改角色页面
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String getUserById(@PathVariable Integer id,Model model,HttpServletRequest request) throws Exception {
        SysRole sysRole = sysRoleService.getById(id);
        model.addAttribute(sysRole);
        return "sysRole/update";
    }

    /**
     * 修改角色
     * @param sysRole
     * @param session
     * @return
     */
    @PostMapping(value="/update")
    public String update(SysRole sysRole,HttpSession session)throws Exception{
        sysRole.setUpdatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        if(sysRoleService.update(sysRole)){
            return "redirect:/sys/role/list";
        }
        return "sysRole/update";
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping(value="/del/{id}")
    @ResponseBody
    public Object del(@PathVariable(value = "id", required = true) Integer id){
        HashMap<String, String> resultMap = new HashMap<String, String>();
        try {
            if(sysRoleService.deleteById(id)){
                resultMap.put("delResult", "true");
            }
            else{
                resultMap.put("delResult", "false");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return JSONArray.toJSONString(resultMap);
    }


    /**
     * 查询角色编号是否已存储在
     * @param code
     * @return
     */
    @RequestMapping(value="/codeExist")
    @ResponseBody
    public Object odeIsExit(@RequestParam(required = true) String code)throws Exception{
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        // 角色编号不可为空
        if(code == null || "".equals(code.trim())){
            resultMap.put("exist", -1);
            resultMap.put("msg", "角色编号不可以为空");
        }else {
            int count = sysRoleService.getCountByCode(code);
            if(count == 0){
                resultMap.put("exist", 0);
            }
            else{
                resultMap.put("exist", 1);
            }
        }
        return JSONArray.toJSONString(resultMap);
    }
}
